package com.zsinfo.guoss.supplier.activity;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.fragment.ChainBillingFragment;
import com.zsinfo.guoss.supplier.fragment.OverseasBillingFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lixl on 2018/12/5.
 * <p>
 * desc:账单明细列表
 */
public class BillingListActivity extends BaseActivity {

    private TabLayout tablayout;
    private ViewPager viewpager;

    private List<Fragment> fragmentList;
    private List<String> list_Title;
    private int type = 1;

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_billing_list;
    }

    @Override
    protected void initView() {
        setTitle("账单明细");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tablayout = findViewById(R.id.tablayout);
        viewpager = findViewById(R.id.viewpager);

        fragmentList = new ArrayList<>();
        list_Title = new ArrayList<>();
        fragmentList.add(new ChainBillingFragment());
        fragmentList.add(new OverseasBillingFragment());
        list_Title.add("国产类保证金");
        list_Title.add("进口类保证金");
        viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(),this, fragmentList, list_Title));
        tablayout.setupWithViewPager(viewpager);//此方法就是让tablayout和ViewPager联动

        //根据type值，显示Fragment，默认选中某项
        type = getIntent().getIntExtra("type", 1);
        if (type == 1){
            tablayout.getTabAt(0).select();
        } else if (type == 2){
            tablayout.getTabAt(1).select();
        }
    }

    @Override
    protected void initNetData() {

    }

    @Override
    protected void customDestroy() {

    }

    public class MyPagerAdapter extends FragmentPagerAdapter {
        private Context context;
        private List<Fragment> fragmentList;
        private List<String> list_Title;

        public MyPagerAdapter(FragmentManager fm, Context context, List<Fragment> fragmentList, List<String> list_Title) {
            super(fm);
            this.context = context;
            this.fragmentList = fragmentList;
            this.list_Title = list_Title;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return list_Title.size();
        }

        /**
         * //此方法用来显示tab上的名字
         *
         * @param position
         * @return
         */
        @Override
        public CharSequence getPageTitle(int position) {
            return list_Title.get(position);
        }
    }
}
